Let’s Git Started

Prof. Matthew G. Son

University of South Florida

Installation

Git installation: Windows

For using Git/Github and Unix, install wsl2

  1. Turn Windows features on or off

    • Check Virtual Machine Platform
    • Check Windows Subsystem for Linux
    • Restart now

Git installation: Windows

  1. Open command prompt with Administrator privilege
wsl --install
  • by default, Ubuntu LTS 22 will be installed.

If you have already installed wsl previously, check status with wsl —-status and confirm you are using wsl2.

# If you installed wsl 1 previously
wsl --update
wsl --set-default-version 2

Git installation: Windows

  1. From Ubuntu (bash), install git
sudo apt install git

Git installation: Windows

  1. Configure git, your name and email address
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Git installation: Windows

  1. In Positron (VScode)
  • Open command pallete with Ctrl + Shift + P
  • Type “Terminal:Select Default Profile”
  • Choose Ubuntu for your default terminal

Git for macOS

  1. Check if Xcode command line tools is not installed (if installed, skip)
  • Open terminal
xcode-select --install
  1. Configure git, your name and email address
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Note

Important

  • You should also create an account on GitHub. Optionally, register with USF for a student previleges.

  • Check if terminal understands git command. Type git on terminal and confirm no error message.

Github Login

Make sure you are able to login to Github.

Introduction to Git/GitHub

Why do we need Git?

Git

  • Distributed version control system, created by Linus Torvalds (2005)

  • “Track changes” made by team members, merge into main, etc.

  • Considered a “must” for software development, and many data science projects.

  • Has a learning curve, but it’s worth learning even for solo projects.

GitHub

  • An online hosting platform that is based on Git system

  • Easy to browse other repositories (public, free)

  • Others: GitLab, Bitbucket, GitBucket, etc…

Why Git?

  • Version control: track changes, revert to previous versions

  • Collaboration: multiple people can work on the same project

  • Backup: store your project on the cloud

  • Portfolio: showcase your work to potential employers

  • Open source: contribute to other projects

Is Git same as GitHub?

Nope.

  • Git can be done completely locally (w/o internet).

  • GitHub deploys Git to online cloud system.

Does Git/Github work with any files?

Git is primarily designed to handle text files and tracks changes line by line.

  • It can upload binary files (e.g. images, pdfs), but doesn’t track differences like text files.

  • GitHub has a file size limit (100MB)

  • It is intended for code tracking; large data should be stored elsewhere

Git Terminology/workflow

Git shell commands

Git clone

Usually performed for the first time downloading from the remote repository.

git clone <repo_url>

# example
git clone https://github.com/example-user/example-repo.git

Git init

git init is used to initialize a new git repository from current working directory.

  • This command creates a new hidden subdirectory named .git
  • If you want to stop git, just remove this folder.
git init

Stage files

git add stages files for commit.

git add <filename> # stage specific files
git add -A # stage all files
git add -u # stage updated (modified, deleted) files only (not new)
git add . # stage new files only (not updated)

Make commit!

git commit creates your project’s version, or a hash block. You must provide message folling -m.

  • As the name suggests, consider committing a serious process of your job, like signing a doc.
git commit -m "Commit message, describes the work / version"

Set remote repository

git remote add adds remote repository.

  • origin is naming convention for Github remote repo.
git remote add <remote_name> <remote_url>

# example
git remote add origin https://github.com/username/repositoryname

Push to repository

git push sends your commit to the remote repository.

# When pushing for the first time
git push -u origin main 

# From then 
git push

Pull from repository

git pull does two operations altogether:

  • fetch: download changes from the remote server
  • merge: apply changes to your local file
git pull

# if job needes to be separated
git fetch
git merge

Git status / log

git status shows the status of changes as untracked, modified, or staged.

git status

git log shows the history of commits and its corresponding hashes.

git log

Git Reset

When you want to get back to previous commit (version) B

  1. git reset B

  • Changes made after B are unstaged
  • Same as git reset --mixed B
  1. git reset --soft B

  • Changes after B are staged
  1. git reset --hard B

  • Match completely to commit B, all others discarded.

In Class Lab

1) My First Commit and Push to Github

  1. Create your own folder for this class
  • Name the folder nicely

  • Create subfolders as:

  • In R folder, create “my_first_R_code.R” file

1) My First Commit and Push to Github

  1. Initialize Git on the folder
git init

and check the status and log on your directory:

git status
git log

1) My First Commit and Push to Github

  1. Create your remote repository on Github website:
  • Create public repo
  • Name your repository nicely
  • DO NOT ADD README FILE (leave it unchecked)
  • Copy the address somewhere
    • It will look like https://github.com/yourGithubId/Reponame.git

1) My First Commit and Push to Github

  1. Stage files (add files)
git add -A # stage all of files on the current directory

Check your status and see the difference

git status

1) My First Commit and Push to Github

  1. Commit the staged version

It creates a “version”, a check-point of your project.

git commit -m "My first git commit"

Check your status and log also:

git status
git log

1) My First Commit and Push to Github

  1. Name your local, initial branch as “main”
git branch -M main

Check your branches from current directory with

git branch

1) My First Commit and Push to Github

  1. Set remote branch (your github) address as “origin”
git remote add origin https://github.com/yourGithubId/Reponame.git

Browse your branches now again, and check the differences

git branch
git branch -r
git branch -a

1) My First Commit and Push to Github

  1. Push your local commit to upstream (remote repo)
git push -u origin main

Browse your github site that files are uploaded.

https://github.com/yourGithubId/Reponame.git

  • Share the address to me.

Note

By default, git will only track files, not empty folders. Folders are tracked when files are in it.

2) My Second Commit and Push to Github

Now, make changes to your R\>my_first_R_code.R file:

  • Write and modify the file: print("Hello world!") in the code and save.

Then add, commit, and push to the github.

  • Browse history with:
git log

3) Reset changes (Time machine)

Now, suppose your second commit (version) was an error.

You can come back to previous commit by:

git reset <hash>

or

git reset head~1 # reset to 1 step before

At now, ONLY your local computer is back to the time when before the second commit. Your Github remote is in the future status (ahead) yet!

3) Reset changes (Time machine)

If you want your remote to be back to the first commit as well:

git push --force

Note that you should use --force option here.

4) Clone Class folder

  1. Navigate to Classroom folder
cd /path/to/your/local/folder
pwd # check your location
  1. Clone the class folder to your machine
git clone <repository_url>
  • I will be uploading class materials here, and will ask you to upload your submissions here as well.

Github and IDE

When using github from other apps (e.g. Positron, VScode, etc), you’ll use PAT (personal access token) instead of password.

  1. Install package from R and generate token
install.packages(usethis) # package installation
usethis::create_github_token()
  1. Generate token from the page, with no expiration (or long enough)
  • Copy the token to clipboard (you won’t see the token again)
  • Store it somewhere temporarily
  • Use it as your password from your Positron